// import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yi_shop/model/address.dart';
import 'package:yi_shop/route/index.dart';
import 'package:yi_shop/ui/scroll_refresh_config.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/svg.dart';
import 'package:yi_shop/utils/tools.dart';

class AddressManageController extends GetxController {
  RxList<Rx<AddressReceiveModel>> pageList = RxList([]);

  RefreshController refreshController = RefreshController();

  @override
  void onInit() {
    pageList.add(Rx(AddressReceiveModel(
        name: '张三',
        mobile: '135*****032',
        district: '广西壮族自治区北海市铁山港区',
        address: '望京方恒国际中心B座',
        checked: true)));
    initData();
    super.onInit();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 32) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    initData();

    /// 完毕
    refreshController.loadComplete();
  }

  void initData() {
    for (var i = 0; i < 10; i++) {
      pageList.add(Rx(AddressReceiveModel(
          name: '张三', mobile: '135*****032', district: '广西壮族自治区北海市铁山港区', address: '望京方恒国际中心B座')));
    }
  }
}

class AddressManagePage extends GetView<AddressManageController> {
  const AddressManagePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: Text(
          '地址管理',
          style: StylesUtils.customTextStyle(
              fontWeight: FontWeight.w700, fontSize: StylesUtils.fontSize18),
        ),
        actions: [
          IconButton(
              onPressed: () {
                Get.toNamed(RoutePath.addressAdd);
              },
              icon: Icon(
                Icons.add,
                weight: 700,
                size: 30,
                color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFfff),
              ))
        ],
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (_, int index) {
        var row = controller.pageList[index];
        return Obx(() => Container(
              height: 140,
              margin: EdgeInsets.only(top: 15.w),
              decoration: const BoxDecoration(color: Colors.white),
              child: Column(
                children: [
                  Container(
                    height: 100,
                    padding: EdgeInsets.symmetric(horizontal: 15.w),
                    child: Column(
                      children: [
                        Container(
                          padding: EdgeInsets.only(top: 15.w),
                          alignment: Alignment.centerLeft,
                          child: Text(
                            '${row.value.name}       ${row.value.mobile}',
                            style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.only(top: 20.w),
                          alignment: Alignment.centerLeft,
                          child: Row(
                            children: [
                              SvgUtils.assetName('u16965', width: 20, height: 16),
                              Expanded(
                                  child: Text(
                                '  ${row.value.district}${row.value.address}',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ))
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                  Container(
                    height: 40,
                    alignment: Alignment.topCenter,
                    padding: EdgeInsets.symmetric(horizontal: 15.w),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Wrap(
                          spacing: 20,
                          crossAxisAlignment: WrapCrossAlignment.center,
                          alignment: WrapAlignment.start,
                          children: [
                            InkWell(
                              onTap: () {
                                for (var r in controller.pageList) {
                                  if (r == row) {
                                    row.update((val) {
                                      val?.checked = !val.checked;
                                    });
                                  } else {
                                    r.update((val) {
                                      val?.checked = false;
                                    });
                                  }
                                }
                              },
                              child: SvgUtils.assetName(row.value.checked ? 'u12520' : 'u12521',
                                  width: 20, height: 20),
                            ),
                            Text(
                              '默认地址',
                              style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color6666,
                              ),
                            )
                          ],
                        ),
                        Wrap(
                          spacing: 10,
                          crossAxisAlignment: WrapCrossAlignment.center,
                          alignment: WrapAlignment.end,
                          children: [
                            InkWell(
                              onTap: () {
                                Get.toNamed(RoutePath.addressEdit, arguments: {'row': row.value});
                              },
                              child: Text.rich(TextSpan(children: [
                                WidgetSpan(
                                    child: SvgUtils.assetName('u11277', width: 16, height: 16)),
                                TextSpan(
                                    text: ' 编辑',
                                    style:
                                        StylesUtils.customTextStyle(color: ColorsUtils.color6666))
                              ])),
                            ),
                            InkWell(
                              onTap: () {
                                controller.pageList.remove(row);
                              },
                              child: Text.rich(TextSpan(children: [
                                WidgetSpan(
                                    child: SvgUtils.assetName('u16963', width: 16, height: 16)),
                                TextSpan(
                                  text: ' 删除',
                                  style: StylesUtils.customTextStyle(color: ColorsUtils.color6666),
                                )
                              ])),
                            )
                          ],
                        )
                      ],
                    ),
                  )
                ],
              ),
            ));
      },
      itemCount: controller.pageList.length,
    );
  }
}
